﻿@inject IHttpService _httpService
@inject IPopupService PopupService
@inject NavigationManager NavigationManager

<MDCard>
    <DataTableCard TModel="UserOverviewModel" Headers="_headers" ApiUrl="@(_baseUrl)" Title="@($"{_name}列表")" ShowAddButton="false" @ref="dataTableCard">
        <ItemColContent>
            @if (context.Header.Value == "actions")
            {
                <div style="width: 40px;">
                    @if (context.Item.CanComment)
                    {
                        <MIcon Color="error"
                               Class="mr-2"
                               OnClick="()=>EditItemCanComment(context.Item,false)">@IconType.DisableComment.ToIconString()</MIcon>
                    }
                    else
                    {
                        <MIcon Color="success"
                               Class="mr-2"
                               OnClick="()=>EditItemCanComment(context.Item,true)">@IconType.Comment.ToIconString()</MIcon>
                    }
                </div>
            }
            else if (context.Header.Value == nameof(UserOverviewModel.CanComment))
            {
                <CanCommentChip CanComment="context.Item.CanComment" />
            }
            else if (context.Header.Value == nameof(UserOverviewModel.UserName))
            {
                <CnGalWebSite.Shared.MasaComponent.PC.EditRecords.UserCenter.ReviewEditRecords.UserChip Id="@context.Item.Id" Name="@context.Item.UserName" />
            }
            else if (context.Header.Value == nameof(UserOverviewModel.RegistTime))
            {
                @context.Item.RegistTime.ToString("yyyy-MM-dd HH:mm")
            }
            else if (context.Header.Value == nameof(UserOverviewModel.LastOnlineTime))
            {
                @context.Item.LastOnlineTime.ToString("yyyy-MM-dd HH:mm")
            }
            else if (context.Header.Value == nameof(UserOverviewModel.OnlineTime))
            {
                @((context.Item.OnlineTime / 60.0 / 60.0).ToString("0.0"))
            }
            else if (context.Header.Value == nameof(UserOverviewModel.PersonalSignature))
            {
                <div style="white-space: pre-line;word-break: break-all;">
                    @context.Item.PersonalSignature
                </div>
            }
            else
            {
                @context.Value
            }
        </ItemColContent>
    </DataTableCard>

</MDCard>


@code {
    private string _baseUrl = "api/account/ListUsers";
    private string _name = "用户";

    private List<DataTableHeader<UserOverviewModel>> _headers = new List<DataTableHeader<UserOverviewModel>>
{
          new ()
          {
            Text= "Id",
            Align= DataTableHeaderAlign.Start,
            Value= nameof(UserOverviewModel.Id)
          },
          new (){ Text= "用户名", Value= nameof(UserOverviewModel.UserName)},
          new (){ Text= "邮箱", Value= nameof(UserOverviewModel.Email)},
          new (){ Text= "个性签名", Value= nameof(UserOverviewModel.PersonalSignature)},
          new (){ Text= "注册时间", Value= nameof(UserOverviewModel.RegistTime)},
          new (){ Text= "最后在线时间", Value= nameof(UserOverviewModel.LastOnlineTime)},
          new (){ Text= "在线时长（小时）", Value= nameof(UserOverviewModel.OnlineTime)},
          new (){ Text= "贡献值", Value= nameof(UserOverviewModel.DisplayContributionValue)},
          new (){ Text= "积分", Value= nameof(UserOverviewModel.DisplayIntegral)},
          new (){ Text= "留言板", Value= nameof(UserOverviewModel.CanComment)},
          new (){ Text= "操作", Value= "actions",Sortable=false }
    };

    DataTableCard<UserOverviewModel> dataTableCard;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }


    public async Task EditItemCanComment(UserOverviewModel model, bool canComment)
    {
        string operationName = canComment ? "开启留言板" : "关闭留言板";
        try
        {
            var obj = await _httpService.PostAsync<EditSpaceCanComment, Result>("api/comments/EditSpaceCanComment", new EditSpaceCanComment { Ids = new string[]{model.Id}, CanComment = canComment });

            if (obj.Successful == false)
            {
                await PopupService.ToastAsync(operationName + "失败",  AlertTypes.Error);
            }
            else
            {

                await PopupService.ToastAsync(operationName + "成功", AlertTypes.Success);
                dataTableCard.GetDataFromApi();

            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, operationName + "失败");
        }
    }
}
